package com.cstp.web;

import com.cstp.manage.FileManage;
import com.cstp.manage.WxMpManage;
import com.cstp.web.res.Resp;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author watts
 */
@Slf4j
@RestController
@RequestMapping("/api/wechat")
public class WechatController {

    @Resource
    private WxMpManage wxMpManage;

    @Resource
    private FileManage fileManage;

    @PostMapping("/get/accessToken")
    public Resp<String> getAccessToken(@RequestParam String code) {
        return Resp.ok(wxMpManage.getAccessToken(code));
    }

    @SneakyThrows
    @PostMapping(value = "/upload/file/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Resp<?> getAccessToken(
            @RequestHeader("token") String token,
            @RequestPart("files") MultipartFile[] files) {
        return Resp.ok(fileManage.uploadReportResource(List.of(files)));
    }

}
